/**
 * 
 */

function openPhoto(num, email){
	if(num == 0) return;
	
	$.ajax({
		url: '../photo/selectPhoto.action'
		, type: 'POST'
		, dataType: 'json'
		, data: {id: num, email: email}
		, success: function(json){
			var vo = json.photo;
			
			$('#photoViewMain .photoViewFrame img').attr('src', '../photoData/'+vo.savedFile);
			
			if(vo.profileFile == null) 
				$('div.writerPhoto img').attr('src', '../image/profile.png');
			else 
				$('div.writerPhoto img').attr('src', '../photoData/'+vo.profileFile);
			
			$('div.writerPhoto').imgLiquid();
			
			$('a.naviNext').attr('href', 'javascript:openPhoto('+vo.prevId+', "'+email+'")');
			$('a.naviPrev').attr('href', 'javascript:openPhoto('+vo.nextId+', "'+email+'")');
			$('span.writerName').text(vo.nickname);
			$('span.writerInputdate').text(vo.inputdate);
			$('div.photoMenu a.delete').attr('href', 'javascript:deletePhoto('+num+')');
			$('div.photoMenu a.likeit').attr('href', 'javascript:javacsript:likeit('+num+')');
			
			var content = '';
			if(vo.content == null){	vo.content = ''; }
			content = vo.content.replace('\n', '<br>');
			if(vo.kind == 'profile'){
				content = "프로필 사진";
			}
			$('div.photoContent').html(content);
			
			if(vo.likeit != 0){
				$('div.contentLike').show();
				var html = "";
				html += '<i class="fa fa-thumbs-o-up"></i>';
				html += '<span class="likeit">'+vo.likeit+'명</span>이 이 사진을 좋아합니다.';
				$('div.contentLike').html(html);
			}
			else{
				$('div.contentLike').hide();
			}
			
			$('#commentInput').attr('onclick', 'javascript:addComment('+vo.id+')');
			
			// page 1을 보내고 photoNum인 id를 보낸다
			commentLoad(1, vo.id);
		}
	});
	
	var width = $(window).innerWidth();
	var height = $(window).innerHeight();
	
	$('#photoViewMain').css('left', (width/2 - 1024/2)+'px');
	$('#photoViewMain').css('top', (height/2 - 600/2)+'px');
	$('#back').on('click', function(){
		$('body, html').css('overflow', 'auto');
		$('#photoViewMain').css('visibility', 'hidden');
		$('#back').removeClass('background');
		$('#back').off('click');
		$('div.photoView img').attr('src', '');
	});
	
	$('div.photoView img').on('load', function(){
		
		photoHeight = $(this).height();
		photoWidth = $(this).width();
		
		$('div.photoView img').css('margin-left', (300-photoWidth/2)+'px');
		$('div.photoView img').css('margin-top', (300-photoHeight/2)+'px');
		
		$('#back').addClass('background');
		$('#photoViewMain').css('visibility', 'visible');
		$('body, html').css('overflow', 'hidden');
	});
}

function commentLoad(page, id){
	$.ajax({
		url: '../photo/selectPhotoComment.action'
		, type: 'POST'
		, dataType: 'json'
		, data: {page: page, id: id}
		, success: function(json){
			commentVo = json.photoCommentList;
			
			if(commentVo.length == 10){
				var html = '<i class="fa fa-comments-o"></i> <a href="javascript:commentLoad('+(page+1)+', '+id+')">더보기</a>';
				$('div.contentLoad').html(html);
				$('div.contentLoad').show();
			}
			else if(commentVo.length < 10){
				$('div.contentLoad').hide();
			}
			
			// make the comment list
			var comment = '';
			$.each(commentVo, function(i, e){
				comment += '<div class="oneComment">';
				comment += '<div class="userProfile">';
				comment += '<div class="userPhoto">';
				if(e.profileFile == null){
					comment += '<img id="profilePicture" src="../image/profile.png"  />';
				}
				else {
					comment += '<img id="profilePicture" src="../photoData/'+e.profileFile+'" />';
				}
				comment += '</div></div>';
				comment += '<div class="commentContent">';
				comment += '<a href="goHome.action?email='+e.email+'"><span class="userName">'+e.nickname+'</span></a>&nbsp;';
				comment += '<span class="userContent">'+e.content+'</span>&nbsp;&nbsp;';
				comment += '<span class="userInputdate">'+e.inputdate+'</span>';
				comment += '</div></div>';
			});
			if(page != 1){
				var thisCmt = $('div.thisComment').html();
				$('div.thisComment').html('');
				comment += thisCmt;
			}
			else {
				$('div.thisComment').html('');
			}
			
			$('div.thisComment').html(comment); // final
			$('div.userPhoto').imgLiquid();
		}
	});
}

function addComment(id){
	var cmt = $('#cmt').val();
	if(cmt == '') return;
	$.ajax({
		url: '../photo/insertPhotoComment.action'
		, type: 'POST'
		, dataType: 'json'
		, data: {comment: cmt, id: id}
		, success: function(){
			$('#cmt').val('');
			commentLoad(1, id);
		}
	});
}

function likeit(id){
	$.ajax({
		url: '../photo/likeit.action'
		, type: 'POST'
		, dataType: 'json'
		, data: {id: id}
		, success: function(json){
			var likeit = json.likeit;
			$('div.contentLike span.likeit').text(likeit+'명');
			$('div.contentLike').show();
			$('div.photoSumnail[data-photo-num='+id+'] span.likeit').text(likeit);
		}
	});
}

function deletePhoto(id){
	$.ajax({
		url: '../photo/deletePhoto.action'
		, type: 'POST'
		, dataType: 'json'
		, data: {id: id}
		, success: function(){
			$('body, html').css('overflow', 'auto');
			$('#photoViewMain').css('visibility', 'hidden');
			$('#back').removeClass('background');
			$('#back').off('click');
			$('div.photoView img').attr('src', '');
			
			
			var zone = $('#photoZone ul li');
			// 마지막 삭제라면
			if(zone.length == 1){
				$('#photoZone').addClass('noPhoto');
				$('div.noPhoto').removeAttr('id');
				
				var html = '<div class="firstPhoto">';
				html += '<i class="fa fa-camera-retro"></i><br>';
				html += '<span class="firstPhoto">소중한 순간<br>';
				html += '기억하기</span></div>';
				
				$('div.noPhoto').html(html);
				
				$('.firstPhoto').click(function(){
					$('body, html').animate({
						scrollTop: '300px'
					});
					$('div.radio1 div.insertPhoto').trigger('click');
					$('#form1 textarea').focus();
				});
			}
			
			addPhotoEffect(1);
		}
	});
}